package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import thread.WmThreadLocalUtil;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 图片上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //检查参数
        if (!multipartFile.isEmpty() && multipartFile.getSize() != 0) {
            //上传图片到minio  图片名称+后缀
            String fileName = UUID.randomUUID().toString().replace("-", "");
            String originalFilename = multipartFile.getOriginalFilename();
            String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String filePath = null;
            try {
                filePath = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
                log.info("图片上传成功{}", filePath);
            } catch (IOException e) {
                e.printStackTrace();
                log.info("图片上传失败{}", filePath);
            }
            //保存到数据库
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(WmThreadLocalUtil.getWmUserThreadLocal().getId());
            wmMaterial.setUrl(filePath);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setType((short) 0);
            wmMaterial.setCreatedTime(new Date());
            save(wmMaterial);
            //4.返回结果
            return ResponseResult.okResult(wmMaterial);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 文章列表
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult listAll(WmMaterialDto wmMaterialDto) {
        //参数校验
        wmMaterialDto.checkParam();
        //查询
        //分页
        Page<WmMaterial> page = new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize());
        //是否收藏
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        if (wmMaterialDto.getIsCollection() != null && wmMaterialDto.getIsCollection() == 1) {
            lqw.eq(WmMaterial::getIsCollection, wmMaterialDto.getIsCollection());
        }
        //userid
        lqw.eq(WmMaterial::getUserId, WmThreadLocalUtil.getWmUserThreadLocal().getId());
        //时间倒序
        lqw.orderByDesc(WmMaterial::getCreatedTime);
        //返回结果
        page = page(page, lqw);
        ResponseResult result=new PageResponseResult(wmMaterialDto.getPage(), wmMaterialDto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }


}
